//
//  MTEditDataVC.m
//  met
//
//  Created by 李晓俊 on 2019/5/9.
//  Copyright © 2019 李晓俊. All rights reserved.
//

#import "MTEditDataVC.h"
#import "MTAboutUsVC.h"
#import <StoreKit/StoreKit.h>
#import "GYWebviewctl.h"
#import "MTXieYiVC.h"
#import "MTFeedBackVC.h"
@interface MTEditDataVC ()
//@property (weak, nonatomic) IBOutlet UIImageView *headImage;
//@property (weak, nonatomic) IBOutlet UILabel *name;
//@property (weak, nonatomic) IBOutlet UILabel *sex;
//@property (weak, nonatomic) IBOutlet UILabel *phone;

@property (weak, nonatomic) IBOutlet UILabel *banben;
@property (weak, nonatomic) IBOutlet UILabel *huancun;


@end

@implementation MTEditDataVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"";
    NSString *libPath = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)[0];
    CGFloat fileSize=[self folderSizeAtPath:libPath];
    self.huancun.text = [NSString stringWithFormat:@"%.1fM",fileSize];
   NSDictionary *infoDictionary=[[NSBundle mainBundle] infoDictionary];
   NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    self.banben.text = app_Version;

    self.view.backgroundColor = KWhiteColor;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0.01f;
    }
    return 10.0f;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *v = [UIView new];
    return v;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01f;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [UIView new];
    return view;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            //意见
             MTFeedBackVC *vc = GetViewControllerInStoryboard(@"Mine", @"MTFeedBackVC");
            [self.navigationController pushViewController:vc animated:YES];
        }else if (indexPath.row == 1) {
//            //昵称
//            MTEditNameVC *vc = [[MTEditNameVC alloc] init];
//            vc.nameStr = KUSER.nickName;
//            vc.chooseClick = ^(NSString * name) {
//                
//            };
//            [self.navigationController pushViewController:vc animated:YES];
            //隐私
            //隐私条款
            [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            AccountService *acc = [AccountService getObject];
            acc.httpRequestResultBlock = ^(id data,int code){
                [MBProgressHUD hideHUDForView:self.view animated:YES];
                if (code == 1) {
                    if ([[NSString stringWithFormat:@"%@",[data objectForKey:@"model"]]isEqualToString:@"1"]) {
                        MTXieYiVC *vc = GetViewControllerInStoryboard(@"Mine", @"MTXieYiVC");
                        vc.name= @"隐私条款";
                        vc.content = [NSString stringWithFormat:@"%@",[data objectForKey:@"content"]];
                        [self.navigationController pushViewController:vc animated:YES];
                    }else {
                        
                        GYWebviewctl *clt = [[GYWebviewctl alloc] init];
                        clt.gyweb = GYWebviewctlUsr;
                        clt.jumpUrl = [NSString stringWithFormat:@"%@",[data objectForKey:@"content"]];
                        [self.navigationController pushViewController:clt animated:YES];
                    }
                }else {
                    [self.view maketishiToast:data];
                }
            };
            [acc findOthers:@"2"];
        }else if (indexPath.row == 2) {
            //服务协议
            [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            AccountService *acc = [AccountService getObject];
            acc.httpRequestResultBlock = ^(id data,int code){
                [MBProgressHUD hideHUDForView:self.view animated:YES];
                if (code == 1) {
                    if ([[NSString stringWithFormat:@"%@",[data objectForKey:@"model"]]isEqualToString:@"1"]) {
                        MTXieYiVC *vc = GetViewControllerInStoryboard(@"Mine", @"MTXieYiVC");
                        vc.name= @"服务协议";
                        vc.content = [NSString stringWithFormat:@"%@",[data objectForKey:@"content"]];
                        [self.navigationController pushViewController:vc animated:YES];
                    }else {
                        
                        GYWebviewctl *clt = [[GYWebviewctl alloc] init];
                        clt.gyweb = GYWebviewctlFuwu;
                        clt.jumpUrl = [NSString stringWithFormat:@"%@",[data objectForKey:@"content"]];
                        [self.navigationController pushViewController:clt animated:YES];
                    }
                    
                }else {
                    [self.view maketishiToast:data];
                }
            };
            [acc findOthers:@"1"];
        }
        else if (indexPath.row == 4) {
            //缓存
            [self clearHuancun];
        }else if (indexPath.row == 5) {
            
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"确定退出登录?" message:@"" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
            [cancelAction setValue:KBlackColor forKey:@"_titleTextColor"];
            UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"退出登录" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
                //退出
                EMError *error = [[EMClient sharedClient] logout:YES];
                if (!error) {
                    DLog(@"退出成功");
                }
                [UserModel clearModel];
                [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"userinfo"];
                KUSER = [UserInfo mj_objectWithKeyValues:[NSDictionary new]];
                [self logouTotLogin];
            }];
            [alertController addAction:cancelAction];
            [alertController addAction:okAction];
            [self presentViewController:alertController animated:YES completion:nil];
        }
    }
   else if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            //关于
            MTAboutUsVC *vc = GetViewControllerInStoryboard(@"Mine", @"MTAboutUsVC");
            [self.navigationController pushViewController:vc animated:YES];
        }else {
            if (@available(iOS 10.3, *)) {
                [SKStoreReviewController requestReview];
            }else {
                NSString *appURL = @"https://itunes.apple.com/cn/app/id1329349192?action=write-review";
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:appURL]];
            }
        }
        
    }else if (indexPath.section == 3) {
        
    }
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
//    [WRNavigationBar wr_setDefaultNavBarShadowImageHidden:NO];
    [WRNavigationBar wr_setDefaultNavBarBarTintColor:KWhiteColor];
    
//    self.name.text = KUSER.nickName;
//    NSString *sex= @"";
//    if ([[NSString stringWithFormat:@"%@",KUSER.sex] isEqualToString:@"1"]) {
//        sex = @"男";
//    }else {
//        sex = @"女";
//    }
//    self.sex.text = sex;
//    self.phone.text = [NSString stringWithFormat:@"%@",KUSER.mobile];
}

- (void)clearHuancun
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *path = [paths lastObject];
    NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:path];
    for (NSString *p in files) {
        NSError *error;
        NSString *Path = [path stringByAppendingPathComponent:p];
        if ([[NSFileManager defaultManager] fileExistsAtPath:Path]) {
            //清理缓存，保留Preference，里面含有NSUserDefaults保存的信息
            if (![Path containsString:@"Preferences"]) {
                [[NSFileManager defaultManager] removeItemAtPath:Path error:&error];
            }
            [self.view maketishiToast:@"清除成功"];
            NSString *libPath = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)[0];
            CGFloat fileSize=[self folderSizeAtPath:libPath];
            self.huancun.text = [NSString stringWithFormat:@"%.1fM",fileSize];
        }else{
            
        }
    }
}
- (float ) folderSizeAtPath:(NSString*) folderPath{
    NSFileManager* manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:folderPath]) return 0;
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];
    NSString* fileName;
    long long folderSize = 0;
    while ((fileName = [childFilesEnumerator nextObject]) != nil){
        NSString* fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
        folderSize += [self fileSizeAtPath:fileAbsolutePath];
    }
    return folderSize/(1024.0*1024.0);
}
- (long long)fileSizeAtPath:(NSString*) filePath{
    NSFileManager* manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:filePath]){
        return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
    }
    return 0;
}
@end
